﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IBatisNet.DataMapper;
using StateGrid.OperationLog;

namespace StateGrid.ZWHY
{
    /// <summary>
    /// Summary description for ZWHY_DAO
    /// </summary>
    public class ZWHY_DAO
    {
        private ISqlMapper sqlMapper = Mapper.Instance();

        /// <summary>
        /// 日志操作对象
        /// </summary>
        private OperationLog_DAO operationLogDAO = new OperationLog_DAO();

        /// <summary>
        /// 获取所有中文会议分类信息。
        /// </summary>
        /// <returns></returns>
        public IList<ZWHY_FL> getAllZwhyFL()
        {
            return sqlMapper.QueryForList<ZWHY_FL>("SelectAllZWHY_FL", null);
        }
        /// <summary>
        /// 获取所有中文会议索引目录信息。
        /// </summary>
        /// <returns></returns>
        public IList<ZWHY_SYML> getAllZwhySYML()
        {
            return sqlMapper.QueryForList<ZWHY_SYML>("SelectAllZWHY_SYML", null);
        }
        /// <summary>
        /// 获取所有中文会议篇名信息。
        /// </summary>
        /// <returns></returns>
        public IList<ZWHY_PM> getAllZwhyPM()
        {
            return sqlMapper.QueryForList<ZWHY_PM>("SelectAllZWHY_PM", null);
        }

        /// <summary>
        /// 获取一部分中文会议分类信息。
        /// 起始结果行从1开始。最后结果包含结束行。
        /// 例如start=1,end=2，则返回第1行和第2行共2个结果。
        /// 结果排序按主键分类编号。
        /// </summary>
        /// <param name="start">起始结果行</param>
        /// <param name="end">结束结果行</param>
        /// <returns></returns>
        public IList<ZWHY_FL> getSomeZwhyFL(int start, int end)
        {
            IDictionary<String, int> para = new Dictionary<String, int>();
            para.Add("start", start);
            para.Add("end", end);
            return sqlMapper.QueryForList<ZWHY_FL>("SelectSomeZWHY_FL", para);
        }
        /// <summary>
        /// 获取一部分中文会议索引目录信息。
        /// 起始结果行从1开始。最后结果包含结束行。
        /// 例如start=1,end=2，则返回第1行和第2行共2个结果。
        /// 结果排序按主键索引编号。
        /// </summary>
        /// <param name="start">起始结果行</param>
        /// <param name="end">结束结果行</param>
        /// <returns></returns>
        public IList<ZWHY_SYML> getSomeZwhySYML(int start, int end)
        {
            IDictionary<String, int> para = new Dictionary<String, int>();
            para.Add("start", start);
            para.Add("end", end);
            return sqlMapper.QueryForList<ZWHY_SYML>("SelectSomeZWHY_SYML", para);
        }
        /// <summary>
        /// 通过馆藏号查询唯一的索引目录，以得到索引编码
        /// </summary>
        /// <param name="gch">馆藏号</param>
        /// <returns>索引目录</returns>
        public ZWHY_SYML getSingleZwhySYML(String gch)
        {
            return sqlMapper.QueryForObject<ZWHY_SYML>("SelectSingleZWHY_SYML", gch);
        }
        /// <summary>
        /// 获取一部分中文会议篇名信息。
        /// 起始结果行从1开始。最后结果包含结束行。
        /// 例如start=1,end=2，则返回第1行和第2行共2个结果。
        /// 结果排序按主键文件名。
        /// </summary>
        /// <param name="start">起始结果行</param>
        /// <param name="end">结束结果行</param>
        /// <returns></returns>
        public IList<ZWHY_PM> getSomeZwhyPM(int start, int end)
        {
            IDictionary<String, int> para = new Dictionary<String, int>();
            para.Add("start", start);
            para.Add("end", end);
            return sqlMapper.QueryForList<ZWHY_PM>("SelectSomeZWHY_PM", para);
        }
        /// <summary>
        /// 检索中文会议索引目录表。
        /// </summary>
        /// <param name="flbm">限定的分类编码。精确匹配。null或空字符串为不设条件。</param>
        /// <param name="zwmc">限定的索引中文名称。模糊匹配。null或空字符串为不设条件。</param>
        /// <param name="start">返回结果的起始序号。</param>
        /// <param name="end">返回结果的结束序号。</param>
        /// <returns></returns>
        public IList<ZWHY_SYML> queryZwhySYML(String flbm, String zwmc, int start, int end)
        {
            if (flbm != null)
            {
                flbm = flbm + '%';
            }
            if (zwmc != null && zwmc.Trim().Length > 0)
                zwmc = '%' + zwmc + '%';
            IDictionary<String, Object> para = new Dictionary<String, Object>();
            para.Add("start", start);
            para.Add("end", end);
            para.Add("queryFlbm", flbm);
            para.Add("queryZwmc", zwmc);
            return sqlMapper.QueryForList<ZWHY_SYML>("SelectSomeZWHY_SYML", para);
        }
        /// <summary>
        /// 获取以某个条件检索索引目录表的结果总数数目。
        /// </summary>
        /// <param name="flbm">限定的分类编码。精确匹配。null或空字符串为不设条件。</param>
        /// <param name="zwmc">>限定的索引中文名称。模糊匹配。null或空字符串为不设条件。</param>
        /// <returns></returns>
        public int countQueryZwhySYML(String flbm, String zwmc)
        {
            if (flbm != null)
            {
                flbm = flbm + '%';
            }
            if (zwmc != null && zwmc.Trim().Length > 0)
                zwmc = '%' + zwmc + '%';
            IDictionary<String, Object> para = new Dictionary<String, Object>();
            para.Add("queryFlbm", flbm);
            para.Add("queryZwmc", zwmc);
            return sqlMapper.QueryForObject<int>("CountSomeZWHY_SYML", para);
        }
        /// <summary>
        /// 获取指定索引编码的篇名。
        /// </summary>
        /// <param name="sybm">指定的索引编码。空或null表示无限制。</param>
        /// <param name="start">返回结果的起始序号。</param>
        /// <param name="end">返回结果的结束序号。</param>
        /// <returns></returns>
        public IList<ZWHY_PM> queryZwhyPM(String sybm,int start, int end)
        {
            String wjm = null;
            if (null == sybm)
            {
            }
            else
            {
                wjm = sybm + '%';
            }
            IDictionary<String, Object> para = new Dictionary<String, Object>();
            para.Add("start", start);
            para.Add("end", end);
            para.Add("wjm", wjm);
            return sqlMapper.QueryForList<ZWHY_PM>("SelectSomeZWHY_PM", para);
        }

        /// <summary>
        /// 通过中文篇名查询篇目
        /// </summary>
        /// <param name="sybm">索引编码</param>
        /// <param name="zwpm">中文篇名</param>
        /// <returns>所查到的篇目列表</returns>
        public IList<ZWHY_PM> queryZwhyPM(String sybm, String zwpm)
        {
            IDictionary<String, Object> para = new Dictionary<String, Object>();
            para.Add("sybm", sybm);
            para.Add("zwpm", zwpm);
            return sqlMapper.QueryForList<ZWHY_PM>("SelectSingleZWHY_PM", para);
        }

        /// <summary>
        /// 模糊查询指定索引编码、年份、期号的篇名数量，文件名=索引编码-年份-期号-3位流水号.PDF
        /// </summary>
        /// <param name="sybm">索引编码</param>
        /// <param name="nf">年份</param>
        /// <param name="qh">期号</param>
        /// <returns></returns>
        public int countQueryZwhyPM(String sybm)
        {
            String wjm = null;
            if (null == sybm)
            {
            }
            else
            {
                wjm = sybm + '%';
            }

            IDictionary<String, Object> para = new Dictionary<String, Object>();
            para.Add("wjm", wjm);
            return sqlMapper.QueryForObject<int>("CountSomeZWHY_PM", para);
        }
        /// <summary>
        /// 根据分类编码字符串获取分类信息。
        /// </summary>
        /// <param name="flbm"></param>
        /// <returns></returns>
        public ZWHY_FL getZwhyFLByFlbm(String flbm)
        {
            return sqlMapper.QueryForObject<ZWHY_FL>("SelectZWHY_FLByFlbm", flbm);
        }

        /// <summary>
        /// 根据分类名称字符串获取分类信息。
        /// </summary>
        /// <param name="flbm"></param>
        /// <returns></returns>
        public ZWHY_FL getZwhyFLByFlmc(String flmc)
        {
            return sqlMapper.QueryForObject<ZWHY_FL>("SelectZWHY_FLByFlmc", flmc);
        }
        /// <summary>
        /// 根据索引编码获取索引目录信息。
        /// </summary>
        /// <param name="sybm"></param>
        /// <returns></returns>
        public ZWHY_SYML getZwhySYMLBySybm(String sybm)
        {
            return sqlMapper.QueryForObject<ZWHY_SYML>("SelectZWHY_SYMLBySybm", sybm);
        }
        /// <summary>
        /// 根据文件名获取篇名信息。
        /// </summary>
        /// <param name="wjm"></param>
        /// <returns></returns>
        public ZWHY_PM getZwhyPMByWjm(String wjm)
        {
            return sqlMapper.QueryForObject<ZWHY_PM>("SelectZWHY_PMByWjm", wjm);
        }

        /// <summary>
        /// 获取原文件名相同的所有中文会议篇名信息。
        /// </summary>
        /// <returns></returns>
        public IList<ZWHY_PM> getZwhyPMByYwjm(String ywjm)
        {
            return sqlMapper.QueryForList<ZWHY_PM>("SelectZWHY_PMByYwjm", ywjm);
        }


        /// <summary>
        /// 根据中文会议篇名获取篇名信息。
        /// </summary>
        /// <param name="wjm"></param>
        /// <returns></returns>
        public ZWHY_PM getZwhyPMByZwpm(String zwpm)
        {
            return sqlMapper.QueryForObject<ZWHY_PM>("SelectZWHY_PMByWwpm", zwpm);
        }
        /// <summary>
        /// 根据索引编码、年份、期号获取最大文件的篇名信息
        /// </summary>
        /// <param name="sybm">索引编码</param>
        /// <returns></returns>
        public ZWHY_PM getMaxZwhyPM(String sybm)
        {
            if (null == sybm || "".Equals(sybm))
            {
                return null;
            }
         
            //设置匹配文件名
            String wjm = sybm+ "%";
            return sqlMapper.QueryForObject<ZWHY_PM>("SelectMaxZWHY_PM", wjm);
        }
        public ZWHY_SYML getMaxZwhySYML(String flbm, int nf)
        {
            if (null == flbm || "".Equals(flbm))
            {
                return null;
            }
            IDictionary<String, Object> para = new Dictionary<String, Object>();
            para.Add("flbm", flbm);
            para.Add("nf", nf);
            return sqlMapper.QueryForObject<ZWHY_SYML>("SelectMaxZWHY_SYML", para);
        }
        /// <summary>
        /// 根据分类编码删除分类信息。
        /// </summary>
        /// <param name="flbm">删除的行数。</param>
        public int deleteZwhyFLByFlbm(String flbm)
        {
            return sqlMapper.Delete("DeleteZWHY_FLByFlbm", flbm);
        }
        /// <summary>
        /// 根据索引编码删除索引目录信息。
        /// </summary>
        /// <param name="sybm">删除的行数</param>
        public int deleteZwhySYMLBySybm(String sybm)
        {
            return sqlMapper.Delete("DeleteZWHY_SYMLBySybm", sybm);
        }
        /// <summary>
        /// 根据文件名删除中文期刊篇名信息。
        /// </summary>
        /// <param name="wjm">删除的行数</param>
        public int deleteZwhyPMByWjm(String wjm, String uname)
        {
            return sqlMapper.Delete("DeleteZWHY_PMByWjm", wjm);
        }
        /// <summary>
        /// 插入中文会议分类信息。
        /// </summary>
        /// <param name="fl"></param>
        public void insertZwhyFL(ZWHY_FL fl)
        {
            sqlMapper.Insert("InsertZWHY_FL", fl);
        }
        /// <summary>
        /// 插入中文会议索引目录信息。
        /// </summary>
        /// <param name="syml"></param>
        public void insertZwhySYML(ZWHY_SYML syml)
        {
            sqlMapper.Insert("InsertZWHY_SYML", syml);
        }
        /// <summary>
        /// 插入中文会议篇名信息。
        /// </summary>
        /// <param name="pm"></param>
        public void insertZwhyPM(ZWHY_PM pm)
        {
            OperationLogItem operationLog = null;
            operationLog = this.GetOperationLogItem(pm, "INSERT", pm.dlr);
            if (null != operationLog)
            {
                this.operationLogDAO.insertLogItem(operationLog);
            }
            sqlMapper.Insert("InsertZWHY_PM", pm);
        }
        /// <summary>
        /// 按索引编码更新索引目录信息。
        /// 索引编码为主键。
        /// </summary>
        /// <param name="syml"></param>
        /// <returns></returns>
        public bool updateZwhySYMLBySYBM(ZWHY_SYML syml)
        {
            bool result;
            try
            {
                sqlMapper.BeginTransaction();
                sqlMapper.Update("UpdateZWHY_SYML", syml);
                sqlMapper.CommitTransaction();
                result = true;
            }
            catch
            {
                sqlMapper.RollBackTransaction();
                result = false;
            }
            return result;
        }
        /// <summary>
        /// 按分类编码更新分类信息。
        /// 分类编码为主键。
        /// </summary>
        /// <param name="fl"></param>
        /// <returns></returns>
        public bool updateZwhyFLByFLBM(ZWHY_FL fl)
        {
            bool result;
            try
            {
                sqlMapper.BeginTransaction();
                sqlMapper.Update("UpdateZWHY_FL", fl);
                sqlMapper.CommitTransaction();
                result = true;
            }
            catch
            {
                sqlMapper.RollBackTransaction();
                result = false;
            }
            return result;
        }
        /// <summary>
        /// 按文件名更新篇名信息。
        /// 文件名为主键。
        /// </summary>
        /// <param name="pm"></param>
        /// <returns></returns>
        public bool updateZwhyPMByWJM(ZWHY_PM pm, String uname)
        {
            OperationLogItem operationLog = null;
            operationLog = this.GetOperationLogItem(pm, "UPDATE", uname);
            bool result;
            try
            {
                sqlMapper.BeginTransaction();
                sqlMapper.Update("UpdateZWHY_PM", pm);
                sqlMapper.CommitTransaction();
                this.operationLogDAO.insertLogItem(operationLog);
                result = true;
            }
            catch
            {
                sqlMapper.RollBackTransaction();
                result = false;
            }
            return result;
        }

        /// <summary>
        /// 修改已有的文件名。
        /// </summary>
        /// <param name="oldWjm">旧文件名</param>
        /// <param name="newWjm">新文件名</param>
        /// <returns>是否修改成功</returns>
        public bool changeZwhyPMWjm(String oldWjm, String newWjm)
        {
            IDictionary<String, Object> para = new Dictionary<String, Object>();
            para.Add("oldwjm", oldWjm);
            para.Add("newwjm", newWjm);
            bool result = true;
            try
            {
                sqlMapper.Update("ChangeZWHY_PM_wjm", para);
                OperationFile.ReNameFile(oldWjm, newWjm);
            }
            catch
            {
                result = false;
            }
            return result;
        }

        /// <summary>
        /// 验证外文期刊篇名是否已存在
        /// </summary>
        /// <param name="wjm">文件名</param>
        /// <param name="zwpm">中文篇名</param>
        /// <returns>true是验证通过，中文篇名不存在；false验证没通过，中文篇名已存在</returns>
        public Boolean ValidateZWHY_PM(String wjm, String zwpm)
        {
            int position = wjm.LastIndexOf('-');
            String wjmSubString = wjm.Substring(0, position);

            IDictionary<String, String> para = new Dictionary<String, String>();
            para.Add("wjm", wjmSubString + "%");
            para.Add("zwpm", zwpm);

            IList<ZWHY_PM> list = sqlMapper.QueryForList<ZWHY_PM>("ValidateZWHY_PM", para);
            if (null == list)
            {
                return true;
            }
            else
            {
                foreach (ZWHY_PM zwhyPM in list)
                {
                    if (zwhyPM.wjm.Equals(wjm))
                    {
                        continue;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            return true;
        }

        /// <summary>
        /// 规范篇号
        /// </summary>
        /// <param name="ph">篇号</param>
        /// <returns>4位篇号</returns>
        private String FormatPH(int ph)
        {
            String phString = ph.ToString();
            //长度为1
            if (phString.Length == 1)
            {
                phString = "000" + phString;
            }
            //长度为2
            else if (phString.Length == 2)
            {
                phString = "00" + phString;
            }
            //长度为1
            else if (phString.Length == 3)
            {
                phString = "0" + phString;
            }
            phString += ".PDF";
            return phString;
        }

        /// <summary>
        /// 生成日志信息
        /// </summary>
        /// <param name="zwqkPM">中文会议信息</param>
        /// <param name="opType">操作类型</param>
        /// <param name="uname">用户名</param>
        /// <returns></returns>
        private OperationLogItem GetOperationLogItem(ZWHY_PM zwhyPM, String opType, String uname)
        {
            if (null == zwhyPM)
            {
                return null;
            }

            OperationLogItem operationLogItem = new OperationLogItem();
            //操作类型
            operationLogItem.opType = opType;
            //数据库表
            operationLogItem.opTable = "ZWHY_PM";
            //全文页数
            operationLogItem.opPageCount = zwhyPM.ys;

            operationLogItem.opObjectID = "中文会议";
            // 操作涉及的书刊
            operationLogItem.opBookIn = "中文会议";

            // 时间
            operationLogItem.opDate = DateTime.Now;
            // 操作员
            operationLogItem.opPerson = uname;
            //记录操作模式，判断是否属于批量操作。取值范围SINGLE/BATCH。
            operationLogItem.opMode = "SINGLE";
            //操作的内容
            String content = "('" + zwhyPM.sybm + "','" + zwhyPM.wjm + "','" + zwhyPM.ywjm + "','" + zwhyPM.zwpm + "','" + zwhyPM.wwpm + "','" + zwhyPM.zz + "','" + zwhyPM.zzdw + "','" + zwhyPM.zz + "','" + zwhyPM.zzdw + "','" + zwhyPM.zwwz + "','" + zwhyPM.wwwz + "','" + zwhyPM.zwztc + "','" + zwhyPM.wwztc + "','" + zwhyPM.ys + "','" + zwhyPM.gtfl + "','" + zwhyPM.gtfl + "','" + zwhyPM.gch + "','" + zwhyPM.gxrq + "','" + zwhyPM.dlr +  "','" + zwhyPM.gxx+"')";
            operationLogItem.opContent = content;
            return operationLogItem;
        }

        public ZWHY_DAO()
        {
            //
            // TODO: Add constructor logic here
            //
        }
    }
}